package com.dynamixsoftware.printershare.smb.netbios;

import com.dynamixsoftware.printershare.App;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Vector;

class NameServiceClient
{
  private static final int RESOLVER_BCAST = 1;
  private static final int RESOLVER_WINS = 2;
  private int nextNameTrnId;
  private int[] resolveOrder;
  
  NameServiceClient()
  {
    if (NbtAddress.getWINSAddress() == null)
    {
      this.resolveOrder = new int[1];
      this.resolveOrder[0] = 1;
      return;
    }
    this.resolveOrder = new int[2];
    this.resolveOrder[0] = 2;
    this.resolveOrder[1] = 1;
  }
  
  private int getNextNameTrnId()
  {
    int i = 1 + this.nextNameTrnId;
    this.nextNameTrnId = i;
    if ((i & 0xFFFF) == 0) {
      this.nextNameTrnId = 1;
    }
    return this.nextNameTrnId;
  }
  
  private void send(NameServicePacket paramNameServicePacket1, NameServicePacket paramNameServicePacket2, int paramInt)
    throws IOException
  {
    int i = NbtAddress.NBNS.length;
    if (i == 0) {
      i = 1;
    }
    byte[] arrayOfByte1 = new byte[1024];
    byte[] arrayOfByte2 = new byte[1024];
    Vector localVector = App.getBroadcastAdrresses();
    DatagramSocket localDatagramSocket = new DatagramSocket();
    localDatagramSocket.setBroadcast(true);
    int j = 0;
    for (int k = 0;; k++)
    {
      if (j == 0) {}
      label267:
      label273:
      do
      {
        for (;;)
        {
          int m;
          try
          {
            if (k >= localVector.size()) {
              break label267;
            }
            m = i;
          }
          finally
          {
            DatagramPacket localDatagramPacket1;
            DatagramPacket localDatagramPacket2;
            localDatagramSocket.close();
          }
          localDatagramPacket1 = new DatagramPacket(arrayOfByte1, 1024, (InetAddress)localVector.get(k), 137);
          localDatagramPacket2 = new DatagramPacket(arrayOfByte2, 1024);
          paramNameServicePacket1.nameTrnId = getNextNameTrnId();
          paramNameServicePacket2.received = false;
          localDatagramPacket1.setAddress(paramNameServicePacket1.addr);
          localDatagramPacket1.setLength(paramNameServicePacket1.writeWireFormat(arrayOfByte1, 0));
          localDatagramSocket.send(localDatagramPacket1);
          localDatagramSocket.setSoTimeout(paramInt);
          try
          {
            localDatagramSocket.receive(localDatagramPacket2);
            paramNameServicePacket2.readWireFormat(arrayOfByte2, 0);
            paramNameServicePacket2.received = true;
            if ((paramNameServicePacket2.nameTrnId != paramNameServicePacket1.nameTrnId) || (paramNameServicePacket1.questionType != paramNameServicePacket2.recordType)) {}
          }
          catch (InterruptedIOException localInterruptedIOException) {}
        }
        if (paramNameServicePacket2.received)
        {
          j = 1;
          break;
        }
        if (!NbtAddress.isWINS(paramNameServicePacket1.addr)) {
          break;
        }
        if (paramNameServicePacket1.addr == NbtAddress.getWINSAddress()) {
          NbtAddress.switchWINS();
        }
        paramNameServicePacket1.addr = NbtAddress.getWINSAddress();
        m = i;
        break label273;
        localDatagramSocket.close();
        return;
        i = m - 1;
      } while (m > 0);
    }
  }
  
  NbtAddress getByName(Name paramName, InetAddress paramInetAddress)
    throws UnknownHostException
  {
    NameQueryRequest localNameQueryRequest = new NameQueryRequest(paramName);
    NameQueryResponse localNameQueryResponse = new NameQueryResponse();
    if (paramInetAddress != null)
    {
      Vector localVector2 = App.getBroadcastAdrresses();
      localNameQueryRequest.addr = paramInetAddress;
      int i5;
      label80:
      int i3;
      if ((localVector2 != null) && (localVector2.size() > 0))
      {
        i5 = 0;
        if (i5 < localVector2.size())
        {
          if (((InetAddress)localVector2.get(i5)).equals(paramInetAddress)) {
            localNameQueryRequest.isBroadcast = true;
          }
        }
        else {
          i3 = 3;
        }
      }
      label229:
      do
      {
        int i4;
        for (;;)
        {
          try
          {
            send(localNameQueryRequest, localNameQueryResponse, 3000);
            if ((!localNameQueryResponse.received) || (localNameQueryResponse.resultCode != 0)) {
              break label229;
            }
            i4 = -1 + localNameQueryResponse.addrEntry.length;
            if (i4 < 0) {
              break label229;
            }
          }
          catch (IOException localIOException2)
          {
            NbtAddress localNbtAddress2;
            boolean bool;
            localIOException2.printStackTrace();
            throw new UnknownHostException(paramName.name);
          }
          try
          {
            getNodeStatus(localNameQueryResponse.addrEntry[i4]);
            localNameQueryResponse.addrEntry[i4].hostName.srcHashCode = paramInetAddress.hashCode();
            localNbtAddress2 = localNameQueryResponse.addrEntry[i4];
            return localNbtAddress2;
          }
          catch (UnknownHostException localUnknownHostException2)
          {
            i4--;
          }
          i5++;
          break;
          if (paramInetAddress.getAddress()[3] == -1)
          {
            bool = true;
            localNameQueryRequest.isBroadcast = bool;
            break label80;
          }
          bool = false;
        }
        i3--;
      } while ((i3 > 0) && (localNameQueryRequest.isBroadcast));
      throw new UnknownHostException(paramName.name);
    }
    int i = 0;
    int j;
    Vector localVector1;
    label360:
    int k;
    label372:
    int m;
    label375:
    int n;
    for (;;)
    {
      if (i < this.resolveOrder.length) {
        switch (this.resolveOrder[i])
        {
        default: 
          i++;
          break;
        case 1: 
        case 2: 
          j = 0;
          if ((this.resolveOrder[i] == 2) && (paramName.name != "\001\002__MSBROWSE__\002") && (paramName.hexCode != 29))
          {
            localNameQueryRequest.addr = NbtAddress.getWINSAddress();
            localNameQueryRequest.isBroadcast = false;
            if (j == 0) {
              break label514;
            }
            localVector1 = App.getBroadcastAdrresses();
            if (j == 0) {
              break label526;
            }
            k = localVector1.size();
            m = 0;
            if (m >= k) {
              break label571;
            }
            if (j != 0) {
              localNameQueryRequest.addr = ((InetAddress)localVector1.get(m));
            }
            n = 3;
          }
          break;
        }
      }
    }
    for (;;)
    {
      label404:
      int i1 = n - 1;
      if (n > 0)
      {
        int i2;
        for (;;)
        {
          try
          {
            send(localNameQueryRequest, localNameQueryResponse, 3000);
            if ((!localNameQueryResponse.received) || (localNameQueryResponse.resultCode != 0)) {
              break label556;
            }
            i2 = -1 + localNameQueryResponse.addrEntry.length;
            if (i2 < 0) {
              break label549;
            }
          }
          catch (IOException localIOException1)
          {
            NbtAddress localNbtAddress1;
            label514:
            label526:
            n = i1;
          }
          try
          {
            getNodeStatus(localNameQueryResponse.addrEntry[i2]);
            localNameQueryResponse.addrEntry[i2].hostName.srcHashCode = localNameQueryRequest.addr.hashCode();
            localNbtAddress1 = localNameQueryResponse.addrEntry[i2];
            return localNbtAddress1;
          }
          catch (UnknownHostException localUnknownHostException1)
          {
            i2--;
          }
          localNameQueryRequest.isBroadcast = true;
          j = 1;
          break;
          localVector1 = new Vector();
          break label360;
          k = 1;
          break label372;
          break label404;
        }
        label549:
        n = i1;
        continue;
        label556:
        if (this.resolveOrder[i] != 2) {}
      }
      else
      {
        m++;
        break label375;
        label571:
        break;
        throw new UnknownHostException(paramName.name);
      }
      n = i1;
    }
  }
  
  NbtAddress[] getNodeStatus(NbtAddress paramNbtAddress)
    throws UnknownHostException
  {
    NodeStatusResponse localNodeStatusResponse = new NodeStatusResponse(paramNbtAddress);
    NodeStatusRequest localNodeStatusRequest = new NodeStatusRequest(new Name("", 0, null));
    localNodeStatusRequest.addr = paramNbtAddress.getInetAddress();
    int j;
    for (int i = 3;; i = j)
    {
      j = i - 1;
      if (i > 0) {
        try
        {
          send(localNodeStatusRequest, localNodeStatusResponse, 1000);
          if ((!localNodeStatusResponse.received) || (localNodeStatusResponse.resultCode != 0)) {
            continue;
          }
          int k = localNodeStatusRequest.addr.hashCode();
          for (int m = 0; m < localNodeStatusResponse.addressArray.length; m++) {
            localNodeStatusResponse.addressArray[m].hostName.srcHashCode = k;
          }
          return localNodeStatusResponse.addressArray;
        }
        catch (IOException localIOException)
        {
          throw new UnknownHostException(paramNbtAddress.toString());
        }
      } else {
        throw new UnknownHostException(paramNbtAddress.hostName.name);
      }
    }
  }
}


/* Location:           C:\Users\Admin\Desktop\Decompiler\JAD\jd-gui-0.3.6.windows\classes_dex2jar.jar
 * Qualified Name:     com.dynamixsoftware.printershare.smb.netbios.NameServiceClient
 * JD-Core Version:    0.7.0.1
 */